package com.ks.zuul.gateway;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;


import java.util.Arrays;

/**
 * @author hd123
 * @date 2020/8/27 11:09
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter(){
        final UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
        final CorsConfiguration corsConfiguration=new CorsConfiguration();
        corsConfiguration.setAllowedOrigins(Arrays.asList("*"));//原始域，这里放所有的
        corsConfiguration.setAllowedMethods(Arrays.asList("*"));//允许的方法get或post如：Arrays.asList("GET")
        corsConfiguration.setAllowedHeaders(Arrays.asList("*"));//允许的头
        corsConfiguration.setAllowCredentials(true);//是否支持cookie跨域
        corsConfiguration.setMaxAge(300L);//设置跨域的缓存时间：在这个事件段内，相同的跨域请求不再进行检查,long类型
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsFilter(source);

    }
}
